//
//  GoodsHeaderView.m
//  团购菜单
//
//  Created by 伍银荣 on 2021/6/4.
//  Copyright © 2021 com.example. All rights reserved.
//

#import "GoodsHeaderView.h"

#define imageCount 5

@interface GoodsHeaderView ()<UIScrollViewDelegate>
@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, strong) NSTimer *timer;

@end

@implementation GoodsHeaderView

//执行这个方法说明GoodsHeaderView已经从xib加载好了,包括他的子控件也已加载好了,就可以在这里面写scrollView相关的代码了
-(void)awakeFromNib {
    [super awakeFromNib];
    self.scrollView.delegate = self;
    CGFloat imgW = self.scrollView.frame.size.width;
    CGFloat imgH = self.scrollView.frame.size.height;
    for (int i = 0; i < imageCount; i++) {
        UIImageView *imgView = [[UIImageView alloc]init];
        CGFloat imgX = imgW * i;
        imgView.frame = CGRectMake(imgX, 0, imgW, imgH);
        imgView.image = [UIImage imageNamed:[NSString stringWithFormat:@"image%02d",i + 1]];
        [self.scrollView addSubview:imgView];
    }
    self.scrollView.contentSize = CGSizeMake(imgW * imageCount, 0);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    
    [self createPageControl];
    [self createTimer];
}

- (void)createPageControl {
    _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(self.scrollView.frame.origin.x, CGRectGetMaxY(self.scrollView.frame) - 10, self.scrollView.frame.size.width, 0)];
    _pageControl.numberOfPages = imageCount;
    _pageControl.currentPage = 0;
    _pageControl.pageIndicatorTintColor = [UIColor blueColor];
    _pageControl.currentPageIndicatorTintColor = [UIColor redColor];
    [self addSubview:self.pageControl];
    
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    NSLog(@"----正在滚动-----");
    CGFloat offset = scrollView.contentOffset.x + scrollView.frame.size.width * 0.5;
    NSInteger page = offset / scrollView.frame.size.width;
    _pageControl.currentPage = page;
}

- (void)createTimer {
    //创建计时器
    _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 repeats:YES block:^(NSTimer * _Nonnull timer) {
        NSInteger page = self.pageControl.currentPage;
        if (page == self.pageControl.numberOfPages - 1) {
            page = 0;
        } else {
            page++;
        }
        CGFloat offset = page * self.scrollView.frame.size.width;
        self.scrollView.contentOffset = CGPointMake(offset, 0);
    }];
    NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
    [runLoop addTimer:_timer forMode:NSRunLoopCommonModes];
    
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    NSLog(@"开始拖拽");
    [_timer invalidate];
    _timer = nil;
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    NSLog(@"结束拖拽");
    //再重新开启定时器
    [self createTimer];
}

+(instancetype)goodsHeaderView {
    GoodsHeaderView *headerView = [[[NSBundle mainBundle]loadNibNamed:@"GoodsHeaderView" owner:nil options:nil] lastObject];
    return headerView;
}
    

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
